package com.atguigu.day03;

import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

public class Flink05_Transfrom_Connect {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.分别获取两个流
        DataStreamSource<Integer> intStream = env.fromElements(1, 2, 3, 4, 5);
        DataStreamSource<String> strStream = env.fromElements("a", "b", "c", "d");

        //TODO 3.合并两条流  注意！！！ 只能合并两条流 并且两个流的类型可以不一样
        ConnectedStreams<Integer, String> connect = intStream.connect(strStream);

        SingleOutputStreamOperator<String> map = connect.map(new CoMapFunction<Integer, String, String>() {
            /**
             * 这个map是用来处理数字流的
             * @param value
             * @return
             * @throws Exception
             */
            @Override
            public String map1(Integer value) throws Exception {
                return value * value + "";
            }

            /**
             * 这个map是用来处理另外一条字母流的
             * @param value
             * @return
             * @throws Exception
             */
            @Override
            public String map2(String value) throws Exception {
                return value + "睁睁眼";
            }
        });

        map.print();

        env.execute();

    }
}
